<?php 
namespace Material\Controller;
use Think\Controller;
class ImageController extends Controller {
	protected $app; //针对应用
	protected $qiniu; //七牛接口对象
	protected $extArr;
	protected $m;
	
	function _initialize() {
		$this->app = array('appid'=>C('DEFAULT_APPID'));
		$config = C('QINIU_CONFIG');
		$this->qiniu = new \Think\Upload\Driver\Qiniu($config);
		//dump($this->qiniu);
		
		//定义允许上传的文件扩展名
		$this->extArr = array(
				'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
				'flash' => array('swf', 'flv'),
				'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
				'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
		);
	
		$this->m = D('MaterialImages');
		
	}
	
	/**
	 * 上传图片到七牛空间
	 */
	public function upload(){
		if(!empty($_FILES)){
			$max_size = 1000000;
			$tmpfile = $_FILES['imgFile'];
			if($_GET['dofor'] == 'markdown'){
				$tmpfile = $_FILES['editormd-image-file'];
			}
			//原文件名
			$file_name = $tmpfile['name'];
			//服务器上临时文件名
			$tmp_name = $tmpfile['tmp_name'];
			//文件大小
			$file_size = $tmpfile['size'];
			//检查文件名
			if (!$file_name) {
				echo $this->_alert("请选择文件。", $_GET['dofor']);
				return;
			}
			//检查文件大小
			if ($file_size > $max_size) {
				echo $this->_alert("上传文件大小超过限制。", $_GET['dofor']);
				return;
			}
				
			//获得文件扩展名
			$temp_arr = explode(".", $file_name);
			$file_ext = array_pop($temp_arr);
			$file_ext = trim($file_ext);
			$file_ext = strtolower($file_ext);
			//检查扩展名
			if (in_array($file_ext, $this->extArr['image']) === false) {
				echo $this->_alert("上传文件扩展名是不允许的扩展名。\n只允许' . implode(",", $this->extArr['image']) . '格式。", $_GET['dofor']);
				return;
			}
				
			$str = new \Org\Util\String();
				
			$file['savename'] = C('SERVER_KEY').$str->keyGen().'.'.$file_ext;
			$file['savepath'] = '';
			$file['tmp_name'] = $tmp_name;
				
				
			//上传到七牛服务器
			if($this->qiniu->save($file)){
				//存储数据组装
				$data['appid'] = $this->app['appid'];
				$data['key'] = $file['savename'];
				$data['url'] = $this->qiniu->downLink($file['savename']);
				$data['create_time'] = date('Y-m-d H:i:s');
				$data['size'] = $file_size;
				if(1){
					$imgID = $this->m->add($data);
					if($imgID){
						if($_GET['dofor'] == 'markdown'){
							echo '{"success":1,"message":"上传成功","url":"'.$data['url'].'"}';
							die();
						}else{
							echo '{"error":0,"url":"'.$data['url'].'","time":"'.$data['create_time'].'","id":'.$imgID.'}';
							die();
						}
					}else{
						echo $this->_alert("数据存储失败。", $_GET['dofor']);//上传异常，输出异常信息
						die();
					}
				}else{
					echo $this->_alert("存储数据组装异常。", $_GET['dofor']);
					die();
				}
			}else{
				echo $this->_alert("服务器上传失败，请检查您的七牛配置。", $_GET['dofor']);
				die();
			}
				
		}
	}
	//返回异常
	public function _alert($msg,$dofor){
		if($dofor == 'markdown'){
			return '{"success":0,"message":"'.$msg.'"}';
		}else{
			return '{"error":1,"message":"'.$msg.'"}';
		}
	
	}
	public function del(){
		$id = array_unique((array)I('ids',0));
		if ( empty($id) ) {
			$this->error('请选择要操作的数据!');
		}
		$map = array($this->m->getPk() => array('in', $id));
		$lists = $this->m->where($map)->select();
		if($this->m->where($map)->delete()){ //删除本地数据库中的row
			foreach($lists as $vo){
				$this->qiniu->del($vo['key']); //删除七牛远程图片
			}
			$this->success('删除成功');
		} else {
			$this->error('删除失败！');
		}
	}
	
	public function index(){
		/*
		if(!empty($_FILES)){
			import('@.ORG.Qiniu.Qiniu');
			$config = array(
					'secrectKey'     => 'cyNowUCcyI2G703sY_42rUiM-L0YFNKX4LzwsZOR', //七牛服务器
					'accessKey'      => 'XjViUn14Ts5dzHJtMD0o8vFdE2iNlfX8oYsXj3G9', //七牛用户
					'domain'         => '7nj15j.com1.z0.glb.clouddn.com', //七牛密码
					'bucket'         => 'misi', //空间名称
					'timeout'        => 300, //超时时间
			);
				
			$qiniu = new Qiniu($config);
			dump($_FILES['file']);
			$file['savename'] = uniqid().'.png';
			$file['savepath'] = '';
			$file['tmp_name'] = $_FILES['file']['tmp_name'];
			//dump($qiniu->save($file));
			dump($qiniu->del('.565d92ad4a27d.png'));
			dump($qiniu->info('.565d92ad4a27d.png'));
			dump($qiniu->downLink('565d94d161bd2.png'));
		}
		*/
	
	}
}